package com.xueyi.aixihu.service.impl;

import com.xueyi.aixihu.entity.vo.DtPictureAuditVo;
import com.xueyi.aixihu.entity.vo.DtPictureCountVo;
import com.xueyi.aixihu.entity.vo.DtUsersVo;
import com.xueyi.aixihu.mapper.DtPictureMessageMapper;
import com.xueyi.aixihu.reqeust.DtFindByAuditPictureReq;
import com.xueyi.aixihu.reqeust.DtUpdatePictureReq;
import com.xueyi.aixihu.reqeust.SendPictureAndDescribeReq;
import com.xueyi.aixihu.response.DtPictureAuditResponse;
import com.xueyi.aixihu.response.base.BaseResponse;
import com.xueyi.aixihu.service.DtPictureMessageService;
import com.xueyi.aixihu.status.SystemCode;
import com.xueyi.aixihu.utils.LogUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yuyu
 * @date : 2022/8/16
 * @Description : 图片审核的实现类
 */
@Service
public class DtPictureMessageServiceImpl implements DtPictureMessageService {

    @Autowired
    private DtPictureMessageMapper dtPictureMessageMapper;

    @Autowired
    HttpServletRequest request;


    /**
     * 查询图片审核中未审核的信息
     *
     * @return
     */
    @Override
    public DtPictureAuditResponse findByWaitAuditPicture(DtFindByAuditPictureReq dtFindByAuditPictureReq) {
        //创建响应类对象
        DtPictureAuditResponse dtPictureAuditResponse = new DtPictureAuditResponse();

        //初始化参数
        dtFindByAuditPictureReq.countStartIndex();
        //获取所有条数
        Integer count = dtPictureMessageMapper.findByWaitAuditPictureCount(dtFindByAuditPictureReq, null);
        //获取图片管理集合
        List<DtPictureAuditVo> byWaitAuditPicture = dtPictureMessageMapper.findByWaitAuditPicture(dtFindByAuditPictureReq, null);
        //判断当前的条数大于0
        if (byWaitAuditPicture.size() > 0 && count > 0) {

            dtPictureAuditResponse.setAuditVoList(byWaitAuditPicture);

            dtPictureAuditResponse.setTotal(count);

            dtPictureAuditResponse.setCode(SystemCode.SUCCESS);

            dtPictureAuditResponse.setMsg("查询成功");

        } else {
            dtPictureAuditResponse.setCode(SystemCode.SUCCESS);

            dtPictureAuditResponse.setMsg("查询为空");

        }
        return dtPictureAuditResponse;
    }

    /**
     * 修改图片状态
     *
     * @param dtUpdatePictureReq
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public BaseResponse updateIsValid(DtUpdatePictureReq dtUpdatePictureReq) {
        String str = "";
        List<DtPictureAuditVo> dtPictureAuditVos = new ArrayList<>();
        if (!StringUtils.isBlank(dtUpdatePictureReq.getId())) {
            //进行修改数据库
            dtPictureMessageMapper.updateIsValid(dtUpdatePictureReq);
            //调用查询这个图片数据的mapper
            String id = dtUpdatePictureReq.getId();
            String[] split = id.split(",");
            for (String s : split) {
                DtPictureAuditVo byOnePicture = dtPictureMessageMapper.findByOnePicture(Integer.parseInt(s));
                dtPictureAuditVos.add(byOnePicture);
            }
            for (DtPictureAuditVo dtPictureAuditVo : dtPictureAuditVos) {
                dtPictureAuditVo.getTitle();
                str = str + dtPictureAuditVo.getTitle() + ",";
            }
            //添加日志
            LogUtil.buildUserLog(dtUpdatePictureReq.getId(), "【图片管理】用户" + DtUsersVo.findCurrentUser().getUsername() + "修改了由" + str + "图片的状态", DtUsersVo.findCurrentUser(), null);
            return BaseResponse.build(SystemCode.SUCCESS, "修改成功", null);
        } else {
            return BaseResponse.build(SystemCode.QUERY_CHECK_ERROR, "id不能为空", null);
        }
    }

    /**
     * 查询已通过跟未通过的数量
     *
     * @return
     */
    @Override
    public BaseResponse<DtPictureCountVo> findByPictureCount() {
        DtPictureCountVo dtPictureCountVo = dtPictureMessageMapper.findByPictureCount();
        return BaseResponse.success(dtPictureCountVo);
    }

    /**
     * 发送描述和图片
     *
     * @param sendPictureAndDescribeReq
     */
    @Override
    public BaseResponse<Boolean> insertPictureAndDescribe(SendPictureAndDescribeReq sendPictureAndDescribeReq) {

        //创建时间
        sendPictureAndDescribeReq.setCreateTime(System.currentTimeMillis());
        dtPictureMessageMapper.insertPictureAndDescribe(sendPictureAndDescribeReq);

        return BaseResponse.build(SystemCode.SUCCESS, "发送成功", Boolean.TRUE);
    }
}
